/*
 * Copyright [2013-2021], Alibaba Group Holding Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.alibaba.polardbx.group.jdbc;

import com.alibaba.polardbx.atom.jdbc.TStatement;
import com.alibaba.polardbx.common.utils.logger.Logger;
import com.alibaba.polardbx.common.utils.logger.LoggerFactory;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.Statement;

/**
 * @author 梦实 2017年11月7日 下午2:15:15
 * @since 5.0.0
 */
public class TGroupDirectStatement implements TStatement {

    private static final Logger log = LoggerFactory.getLogger(TGroupDirectStatement.class);

    protected TGroupDirectConnection tGroupConnection;
    protected String sql;

    private final Statement targetStatement;

    private InputStream infileInputStream;

    public TGroupDirectStatement(TGroupDirectConnection conn, Statement stmt) {
        this.tGroupConnection = conn;
        this.targetStatement = stmt;
        conn.setCurrentStatement(stmt);

    }

    public void addBatch(String sql) throws SQLException {
        targetStatement.addBatch(sql);
    }

    public void cancel() throws SQLException {
        targetStatement.cancel();
    }

    public void clearBatch() throws SQLException {
        targetStatement.clearBatch();
    }

    public void clearWarnings() throws SQLException {
        targetStatement.clearWarnings();
    }

    public void close() throws SQLException {
        targetStatement.close();
    }

    public void closeOnCompletion() throws SQLException {
        targetStatement.closeOnCompletion();
    }

    public boolean execute(String sql) throws SQLException {
        return targetStatement.execute(sql);
    }

    public boolean execute(String sql, int autoGeneratedKeys) throws SQLException {
        return targetStatement.execute(sql, autoGeneratedKeys);
    }

    public boolean execute(String sql, int[] columnIndexes) throws SQLException {
        return targetStatement.execute(sql, columnIndexes);
    }

    public boolean execute(String sql, String[] columnNames) throws SQLException {
        return targetStatement.execute(sql, columnNames);
    }

    public int[] executeBatch() throws SQLException {
        return targetStatement.executeBatch();
    }

    public long[] executeLargeBatch() throws SQLException {
        return targetStatement.executeLargeBatch();
    }

    public long executeLargeUpdate(String sql) throws SQLException {
        return targetStatement.executeLargeUpdate(sql);
    }

    public long executeLargeUpdate(String sql, int autoGeneratedKeys) throws SQLException {
        return targetStatement.executeLargeUpdate(sql, autoGeneratedKeys);
    }

    public long executeLargeUpdate(String sql, int[] columnIndexes) throws SQLException {
        return targetStatement.executeLargeUpdate(sql, columnIndexes);
    }

    public long executeLargeUpdate(String sql, String[] columnNames) throws SQLException {
        return targetStatement.executeLargeUpdate(sql, columnNames);
    }

    public ResultSet executeQuery(String sql) throws SQLException {
        return targetStatement.executeQuery(sql);
    }

    public int executeUpdate(String sql) throws SQLException {
        return targetStatement.executeUpdate(sql);
    }

    public int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException {
        return targetStatement.executeUpdate(sql, autoGeneratedKeys);
    }

    public int executeUpdate(String sql, int[] columnIndexes) throws SQLException {
        return targetStatement.executeUpdate(sql, columnIndexes);
    }

    public int executeUpdate(String sql, String[] columnNames) throws SQLException {
        return targetStatement.executeUpdate(sql, columnNames);
    }

    public Connection getConnection() throws SQLException {
        return targetStatement.getConnection();
    }

    public int getFetchDirection() throws SQLException {
        return targetStatement.getFetchDirection();
    }

    public int getFetchSize() throws SQLException {
        return targetStatement.getFetchSize();
    }

    public ResultSet getGeneratedKeys() throws SQLException {
        return targetStatement.getGeneratedKeys();
    }

    public long getLargeMaxRows() throws SQLException {
        return targetStatement.getLargeMaxRows();
    }

    public long getLargeUpdateCount() throws SQLException {
        return targetStatement.getLargeUpdateCount();
    }

    public int getMaxFieldSize() throws SQLException {
        return targetStatement.getMaxFieldSize();
    }

    public int getMaxRows() throws SQLException {
        return targetStatement.getMaxRows();
    }

    public boolean getMoreResults() throws SQLException {
        return targetStatement.getMoreResults();
    }

    public boolean getMoreResults(int current) throws SQLException {
        return targetStatement.getMoreResults(current);
    }

    public int getQueryTimeout() throws SQLException {
        return targetStatement.getQueryTimeout();
    }

    public ResultSet getResultSet() throws SQLException {
        return targetStatement.getResultSet();
    }

    public int getResultSetConcurrency() throws SQLException {
        return targetStatement.getResultSetConcurrency();
    }

    public int getResultSetHoldability() throws SQLException {
        return targetStatement.getResultSetHoldability();
    }

    public int getResultSetType() throws SQLException {
        return targetStatement.getResultSetType();
    }

    public int getUpdateCount() throws SQLException {
        return targetStatement.getUpdateCount();
    }

    public SQLWarning getWarnings() throws SQLException {
        return targetStatement.getWarnings();
    }

    public boolean isCloseOnCompletion() throws SQLException {
        return targetStatement.isCloseOnCompletion();
    }

    public boolean isClosed() throws SQLException {
        return targetStatement.isClosed();
    }

    public boolean isPoolable() throws SQLException {
        return targetStatement.isPoolable();
    }

    public boolean isWrapperFor(Class<?> iface) throws SQLException {
        return targetStatement.isWrapperFor(iface);
    }

    public void setCursorName(String name) throws SQLException {
        targetStatement.setCursorName(name);
    }

    public void setEscapeProcessing(boolean enable) throws SQLException {
        targetStatement.setEscapeProcessing(enable);
    }

    public void setFetchDirection(int direction) throws SQLException {
        targetStatement.setFetchDirection(direction);
    }

    public void setFetchSize(int rows) throws SQLException {
        targetStatement.setFetchSize(rows);
    }

    public void setLargeMaxRows(long max) throws SQLException {
        targetStatement.setLargeMaxRows(max);
    }

    public void setMaxFieldSize(int max) throws SQLException {
        targetStatement.setMaxFieldSize(max);
    }

    public void setMaxRows(int max) throws SQLException {
        targetStatement.setMaxRows(max);
    }

    public void setPoolable(boolean poolable) throws SQLException {
        targetStatement.setPoolable(poolable);
    }

    public void setQueryTimeout(int seconds) throws SQLException {
        targetStatement.setQueryTimeout(seconds);
    }

    public <T> T unwrap(Class<T> iface) throws SQLException {
        return targetStatement.unwrap(iface);
    }

    @Override
    public void setLocalInfileInputStream(InputStream stream) {
        this.infileInputStream = stream;
    }

    @Override
    public InputStream getLocalInfileInputStream() {
        return this.infileInputStream;
    }

    @Override
    public String getAppName() {
        return this.tGroupConnection.getGroupDataSource().getAppName();
    }

}
